<?php
namespace home\controller;
header("content-type:text/html;charset=utf-8");
use \core\Controller;// 引入空间类 全局空间下的 core空间下的Controller类
class IndexController extends Controller{
    //展示博客首页(列表页)
    public function showIndex(){
  //渲染模板
        $articleModel=SF('\\model\\ArticleModel');

        //接受表单数据
        $s_title=isset($_POST['s_title'])?addslashes(trim($_POST['s_title'])):null;
        //拼接查询条件
        $condition='';
        if($s_title!=null){
            $condition.= 'a.title like "%'.$s_title.'%"';
        }
        //分页功能
        $nowpage=empty($_GET['page'])?1:$_GET['page'];
        $num_per_page=1;//每页总数

        $total_rows=$articleModel->all_rows($condition); //总行数
        $total_page=intval(ceil($total_rows['num']/$num_per_page));//总页数
        $x=($nowpage-1)*$num_per_page;

        $condition=empty($condition)?1:$condition;
        $condition1="$condition limit $x,$num_per_page";

        $rows=$articleModel->joinCat($condition1);
        $url=C('url.main').'?p=home&m=Index&a=showIndex&page';
        $strPage=pageHtml($nowpage,$total_page,$url);

        $this->assign('rows',$rows);
        $this->assign('strPage',$strPage);






    //做无限级分类
        $cat_model=SF('\\model\\CategoryModel');
        $catRows=$cat_model->getRows('id,name,parent_id','bl_category',1);
        $tree=array();
        $cat_model->recursiveCat($tree,$catRows);

        $this->assign('tree',$tree);
        $this->display('blogShowList.html');



}

    //展示博文详情页
    public function showOne()
    {

        $id = $_GET['id'];
        $articleModel = SF('\\model\\ArticleModel');
        $row = $articleModel->getRow('id,cat_id,title,intro,content,pubtime,ad_account,ad_id', 'bl_article', 'id=' . $id);

        $row['content'] = htmlspecialchars_decode($row['content']);
        $catModel = SF('\\model\\ArticleModel');
        $catRows = $catModel->getRow('id,name,parent_id', 'bl_category', 'id=' . $row['cat_id']);

        //判断点赞系统
        @$add_id=$_GET['cc'];
        if($add_id==1){
            $sql='ad_id=ad_id+1';

           $aa=$catModel->upd( 'bl_article',$sql,'id=' . $id);
echo $aa;
exit;
            header('Location:http://www.mvc.com/index.php?p=home&m=Index&a=showOne&id='.$id);
            echo 'ok';
        }


        $row['cat_id'] = $catRows['name'];

        $this->assign('row', $row);
        //渲染模板
        $this->display('blogShow.html');
    }



    //会员登录判断
    public function chk_login(){
        //接受信息
        $user=$_POST['user'];
        $pwd=$_POST['pwd'];
//        var_dump($user);
//        exit;
        $chkModel=SF('\\model\\MemberModel'); //调用数据库
        $sql=$chkModel->getRow('accout,pwd','bl_member',"accout='$user'");//调用方法

        //判断是否为空
        if(!empty($sql)){
            if($pwd==$sql['pwd']){
                $this->jump('登录成功','p=home&m=Index&a=showIndex',1);
                exit;
            }
        }

        $this->jump('密码或账号不匹配','p=home&m=Index&a=showIndex',1);
    }


}